package com.yc.cloud.openai.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.yc.cloud.basic.exception.Asserts;
import com.yc.cloud.openai.constants.HFJKConstants;
import com.yc.cloud.openai.dto.NLPResultDTO;
import com.yc.cloud.openai.dto.NlpChunkResultDto;
import com.yc.cloud.openai.dto.resp.HFKJNLPResp;
import com.yc.cloud.openai.request.NLPParam;
import com.yc.cloud.openai.service.NLPService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.util.Map;

@Service("HFKJNLPServiceImpl")
@Slf4j
public class HFKJNLPServiceImpl implements NLPService {

    // API URL
    private static final String apiUrl = "https://api.link-ai.chat/v1/chat/completions";
    // 构建请求头
    private static final String authorizationToken = "Bearer Link_t6B2FIbedKMTvsDjFPztyVSc4w2tVMtQLUkIVNRmiW";


    @Override
    public NLPResultDTO getNLPResult(NLPParam param) {
        // 构建请求体
        val startTime = System.currentTimeMillis();
        val jsonArray = JSONUtil.createArray();
        jsonArray.add(JSONUtil.createObj().set("role", "user").set("content", param.getContent()));
        String requestBody = JSONUtil.createObj().set("app_code", "pP2Mwruj").set("messages", jsonArray).toString();
        log.info("HFKJNLP requestBody--->{}", requestBody);
        // 发送 POST 请求
        try (HttpResponse response = HttpRequest.post(apiUrl).header("Content-Type", "application/json") // 设置内容类型为 JSON
                .header("Authorization", authorizationToken) // 设置认证头
                .body(requestBody) // 设置请求体
                .execute()) {
            val nlpResult = response.body();
            val endTime = System.currentTimeMillis();
            log.info("HFKJNLP request cost time--->{} ms", endTime - startTime);
            log.info("HFKJNLP RESULT--->{}", nlpResult);
            val nlpResp = JSONUtil.toBean(nlpResult, HFKJNLPResp.class);
            val choiceList = nlpResp.getChoices();
            //没有任何选择,直接返回空
            if (CollUtil.isEmpty(choiceList)) {
                return null;
            }
            val result = new NLPResultDTO();
            val costTime = endTime - startTime;
            result.setCostTime(costTime);
            result.setResult(choiceList.get(0).getMessage().getContent());
            //直接拿第一个选项内容作为结果
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            Asserts.fail(HFJKConstants.NLP_SERVICE_ERROR);
        }
        return null;
    }

    @Override
    public Flux<String> getNLPResultByStream(NLPParam param) {
        return null;
    }

    @Override
    public Flux<ServerSentEvent<String>> getNLPResultByStream2(NLPParam param) {
        return null;
    }

    @Override
    public SseEmitter getNLPResultByStream3(NLPParam nlpParam) {
        return null;
    }

}
